/* deploy.groovy
    ##################################################
    # Created by zy                        #
    # Plugin install:  build user vars, maven node build                                                #
    # A Part of the Project jenkins-library          #
    ##################################################
*/

package org.devops
def k8sDeploy(k8scred, env_type, image_name, deploy_yaml){
    println "[INFO] 执行k8sDeploy"
    withCredentials([file(credentialsId: k8scred, variable:"vault")]) {
        sh """
            echo "[INFO] 部署${env_type}环境"
            if [ ! -d ~/.kube ];then
                mkdir ~/.kube
                fi
            echo $vault| xargs cat > ~/.kube/config
            sed -i 's@IMAGE_NAME@${image_name}@g' $deploy_yaml
            kubectl apply -f $deploy_yaml
        """
    }
}
def k8sVueDeploy(k8scred, vue_k8sserver_path, k8s_namespace){
    println "[INFO] 执行k8sVueDeploy"
    withCredentials([file(credentialsId: k8scred, variable:"vault")]) {
        sh """
            if [ ! -d ~/.kube ];then
                mkdir ~/.kube
            fi
            echo $vault| xargs cat > ~/.kube/config
            kubectl cp dist nginx-0:$vue_k8sserver_path -n $k8s_namespace
        """
    }
}

def sshDockerDeploy(ssh_name, ssh_host, ssh_port, ssh_cred, dhub_cred, registry, image_name, d_compose_path){
    println "[INFO] 执行sshDockerDeploy"
    def remote = [:]
    remote.name = ssh_name
    remote.host = ssh_host
    remote.port = ssh_port
    remote.allowAnyHosts = true
    withCredentials([usernamePassword(credentialsId: ssh_cred, passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote.user = "${userName}"
        remote.password = "${password}"
    }
    withCredentials([usernamePassword(credentialsId: $dhub_cred, passwordVariable: 'password', usernameVariable: 'username')]) {
        sshCommand remote: remote, command: "docker login -u ${username} -p ${password} ${registry}"
        sshCommand remote: remote, command: "echo \"IMAGE_NAME=${image_name}\" > $d_compose_path/.env"
        sshCommand remote: remote, command: "sh $d_compose_path/app_start.sh"
    }
}

def sshVueDeploy(ssh_name, ssh_host, ssh_port, ssh_cred, server_path){
    println "[INFO] 执行sshVueDeploy"
    def remote = [:]
    remote.name = ssh_name
    remote.host = ssh_host
    remote.port = ssh_port
    remote.allowAnyHosts = true
    withCredentials([usernamePassword(credentialsId: ssh_cred, passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote.user = "${userName}"
        remote.password = "${password}"
    }
    sshCommand remote: remote, command: "if [ -d ${server_path}/dist  ]; then rm -rf ${server_path}/dist; fi"
	sshPut remote: remote, from: "dist", into: "${server_path}"
}

def ansibleVueDeploy(ansible_hosts_file, ansible_host, vue_server_path,git_profile_path){
    println "[INFO] 执行ansibleVueDeploy"    
    sh """
        #ansible -i $ansible_hosts_file $ansible_host  -m shell -a "sudo rm -rf ${vue_server_path}/dist"
        #ansible -i $ansible_hosts_file $ansible_host  -m copy -a "src=dist dest=${vue_server_path}"
        ansible -i $ansible_hosts_file $ansible_host  -m shell -a "if [ -d ${vue_server_path}/dist.tar.gz  ]; then sudo rm -rf ${vue_server_path}/dist.tar.gz; fi"
        ansible -i $ansible_hosts_file $ansible_host  -m copy -a "src=${git_profile_path}dist.tar.gz dest=${vue_server_path}"
        ansible -i $ansible_hosts_file $ansible_host  -m shell -a "if [ -d ${vue_server_path}/dist  ]; then sudo rm -rf ${vue_server_path}/dist; fi"
        ansible -i $ansible_hosts_file $ansible_host  -m shell -a "tar xf ${vue_server_path}/dist.tar.gz  -C ${vue_server_path}/"
    """
}

def ansibleMvnDeploy(ansible_hosts_file, ansible_host,image_name, d_compose_path){
    println "[INFO] 执行ansibleMvnDeploy"    
    sh """
        ansible -i $ansible_hosts_file $ansible_host  -m shell -a "sudo echo IMAGE_NAME=$image_name > $d_compose_path/.env"
        ansible -i $ansible_hosts_file $ansible_host  -m shell -a "sudo sh $d_compose_path/app_start.sh"
    """
}


    // private cmd = """
    //       cp -f settings.xml /root/.m2/settings.xml
    //       sed -i 's/nexus.dispute.bangying.tech/nexus.devops:8081/g' ~/.m2/settings.xml
    //       mvn dependency:go-offline -B
    //       # rm -rf /root/.m2/repository/com/univer
    //       mvn clean package -Dmaven.test.skip=true""" + mavenEnvParameter 
    // sh(cmd)

    // withCredentials([file(credentialsId: 'bangying-k8s',variable:"vault")]) {
    //     sh """
    //         echo "[INFO] 部署${params.ENV_TYPE}环境"
    //         if [ ! -d ~/.kube ];then
    //             mkdir ~/.kube
    //             fi
    //         echo $vault| xargs cat > ~/.kube/config
    //         sed -i 's@IMAGE_NAME@${image_name}@g' dispute-pre2-manage-api.yaml
    //         kubectl apply -f dispute-pre2-manage-api.yaml
    //     """
    // }